// pages/Systems/Rankings/Rankings.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { getSumRankList , queryAppInforMation } from '../../../api/brand';
import { formatTime } from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareTitle: app.globalData.global.shareTitle,
    assetsAddress: assetsAddress,
    active: 1,
    gtDate: '', // 大于的日期
    ltDate: '', // 小于的日期
    rankings: [],
    selfRank: '未排名'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ltDate = formatTime(new Date(),'Y-M-D');
    var time = new Date().getTime();
    var lt = new Date(time - 24 * 2 * 3600 *  1000)
    var gtDate = formatTime(new Date(lt),'Y-M-D');
    this.setData({
      ltDate: ltDate,
      gtDate: gtDate,
    })
    this._getSumRankList();
    this._getUserSelfRank();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var ltDate = formatTime(new Date(),'Y-M-D');
    var time = new Date().getTime();
    var lt = new Date(time - 24 * 2 * 3600 *  1000)
    var gtDate = formatTime(new Date(lt),'Y-M-D');
    this.setData({
      ltDate: ltDate,
      gtDate: gtDate,
      active: 1
    })
    this._getSumRankList();
    this._getUserSelfRank();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 选择数据日期
  selDate(e){
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    console.log(item,'item')
    let gtDate = '';
    let ltDate = '';
    if(this.data.active == item){
      return;
    }
    if(item == 1){
      ltDate = formatTime(new Date(),'Y-M-D');
      var time = new Date().getTime();
      var lt = new Date(time - 24 * 2 * 3600 *  1000)
      gtDate = formatTime(new Date(lt),'Y-M-D');
    }
    if(item == 2){
      var day = new Date().getDay();
      console.log(day,'nowdate')
      ltDate = formatTime(new Date(new Date().getTime() - ( (day-1) * 24 * 3600 * 1000)),'Y-M-D');
      gtDate = formatTime(new Date(new Date().getTime() - ( (day + 8)  * 24 * 3600 * 1000)),'Y-M-D');
    }
    if(item == 3){
      var days = new Date().getDate();
      var lastDate = new Date(new Date().getTime() - (days * 24 * 3600 * 1000))
      var lastDates = new Date(new Date(lastDate).getTime() - (new Date(lastDate).getDate() * 24 * 3600 * 1000));
      var lastYear = lastDates.getFullYear();
      var lastMonth = lastDates.getMonth() + 1;
      var lastDay = lastDates.getDate();
      var ltDates = new Date();
      ltDate = `${ltDates.getFullYear()}-${(ltDates.getMonth() + 1) < 10 ? '0' + (ltDates.getMonth() + 1) : (ltDates.getMonth() + 1)}-01`;
      gtDate = `${lastYear}-${lastMonth < 10 ? '0'+lastMonth : lastMonth}-${lastDay < 10 ? '0' + lastDay : lastDay}`;
      console.log(gtDate,ltDate,'gtDategtDate')
    }
    this.setData({
      active: item,
      gtDate: gtDate,
      ltDate: ltDate,
    })
    this._getSumRankList();
    this._getUserSelfRank();
  },
  _getSumRankList(){
    let _this = this;
    const { gtDate , ltDate } = _this.data;
    getSumRankList({gtDate: gtDate,ltDate: ltDate})
    .then(res => {
      if(res.success){
        _this.setData({
          rankings: res.data,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  _getUserSelfRank(){
    let _this = this;
    const { gtDate , ltDate } = _this.data;
    getSumRankList({gtDate: gtDate,ltDate: ltDate,selfRank: '1'})
    .then(res => {
      if(res.success){
        if(res.data.length > 0){
          _this.setData({
            selfRank: `${res.data[0].rank}名`
          })
        }else{
          _this.setData({
            selfRank: '未排名'
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 查看规则
  showRule(){
    let _this = this;
    queryAppInforMation({infoType: '2',title: '排行榜规则',page: _this.data.page})
    .then(res => {
      if(res.success){
        if(res.data.content.length > 0){
          wx.showModal({
            showCancel: false,
            title: '排行榜规则',
            content: res.data.content[0].content,
          })
        }else{
          wx.showToast({
            title: '暂无相关规则信息',
            icon: 'none',
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
})